# -*- coding: utf-8 -*-
"""
Created on Tue Aug 16 09:47:47 2022

@author: wulong
"""
import numpy as np
from casadi import *
from Basic_paras import *

# In[1] Fast EMPC 3 model
# States
def ode_ies_f3(x_f3, xs, x_f1, x_f2, us, u_f1, u_f2, u_f3, uz, ud):
    dxdt = SX.zeros(Nx_f3)
    
    C_soc = xs[0]
    C_sot = xs[1]
    C_stc = xs[2]
    C_sth = xs[3]
    tbr = xs[4]
    
    If = x_f1[0]
    Gh2 = x_f1[1]
    pO2 = x_f1[2]
    pH2O = x_f1[3]
    pH2 = x_f1[4]
    vcap = x_f1[5]
    Iba = x_f1[6]
    
    Pmtf = x_f2[0]
    tabf = x_f2[1]
    tabw = x_f2[2]
    tabt = x_f2[3]
    tewm = x_f2[4]
    tre = x_f2[5]
    
    tc = x_f3[0]
    tcs = x_f3[1]
    tcwm = x_f3[2]
    te = x_f3[3]
    tes = x_f3[4]
    
    Gab = us[0]
    Gec = us[1]
    Gstu = us[2]
    
    Gff = u_f1[0]
    Pbar = u_f1[1]
    
    Gfm = u_f2
    
    Nec = u_f3
    
    zfc = uz[0]
    zma = uz[1]
    zec = uz[2]
    zst = uz[3]
    
    ta = ud[0]
    Sra = ud[1]
    Pd = ud[2]
    Qother = ud[3]
        
    # EC
    pc = exp(21.3-2025.5/(248.94+tc))/1e6
    pe = exp(21.3-2025.5/(248.94+te))/1e6
    hcro = -137.26+1.23463*(273.15+tc) - hspc
    hero = 338.02893+0.24532*(273.15+te) + hsph
    tcwo = 2*tcwm - tcwi
    pr = pc/pe # Compressor
    etavl = 0.98-0.085*(pr**(1/kr)-1)
    Gr = etavl*Nec*roeg*Vcp
    wi = kr/(1e3*(kr-1))*pe*1e6/roeg*(pr**((kr-1)/kr)-1)
    hcri = hero + wi
    heri = hcro # Expansion valve
    
    dxdt[0] = zec*((Gr*(hcri - hcro) + alfar*Aci*(tcs - tc))/(Ccr*Mcr))
    dxdt[1] = zec*((alfar*Aci*(tc - tcs) + alfaw*Aco*(tcwm - tcs))/(Cs*Mcs))
    dxdt[2] = zec*((Cw*Gcw*(tcwi - tcwo) + alfaw*Aco*(tcs - tcwm))/(Cw*Mcw))
    dxdt[3] = zec*((Gr*(heri - hero) + alfar*Aei*(tes - te))/(Cer*Mer))
    dxdt[4] = zec*((alfar*Aei*(te - tes) + alfaw*Aeo*(tewm - tes))/(Cs*Mes))
    
    return dxdt

# Output
def out_ies_f3(x_f3, xs, x_f1, x_f2, us, u_f1, u_f2, u_f3, uz, ud):
    y_alg = MX.zeros(Ny_f3)
    
    C_soc = xs[0]
    C_sot = xs[1]
    C_stc = xs[2]
    C_sth = xs[3]
    tbr = xs[4]
    
    If = x_f1[0]
    Gh2 = x_f1[1]
    pO2 = x_f1[2]
    pH2O = x_f1[3]
    pH2 = x_f1[4]
    vcap = x_f1[5]
    Iba = x_f1[6]
    
    Pmtf = x_f2[0]
    tabf = x_f2[1]
    tabw = x_f2[2]
    tabt = x_f2[3]
    tewm = x_f2[4]
    tre = x_f2[5]
    
    tc = x_f3[0]
    tcs = x_f3[1]
    tcwm = x_f3[2]
    te = x_f3[3]
    tes = x_f3[4]
    
    Gab = us[0]
    Gec = us[1]
    Gstu = us[2]
    
    Gff = u_f1[0]
    Pbar = u_f1[1]
    
    Gfm = u_f2
    
    Nec = u_f3
    
    zfc = uz[0]
    zma = uz[1]
    zec = uz[2]
    zst = uz[3]
    
    ta = ud[0]
    Sra = ud[1]
    Pd = ud[2]
    Qother = ud[3]
    
    # PV
    I_pv_max = I_max_0*Sra/Sra_pv_0*(1 + c_pv1*(ta - T_pv_0))
    V_pv_max = V_max_0*log(exp(1) + c_pv2*(Sra - Sra_pv_0))*(1 - c_pv3*(ta - T_pv_0))
    Ppv = npp*nsp*I_pv_max*V_pv_max/1000
        
    # FC
    eta_a = afc + bfc*log(If + eps)
    eta_c = -R0fc*T0fc/(2*F0)*log(1-If/IL + eps)
    eta_o = If*rfc
    V0 = N0fc*(E0fc + R0fc*T0fc/(2*F0)*log(pH2*sqrt(pO2)/(pH2O + eps) + eps))
    Vfc = V0 - eta_a - eta_c - eta_o
    Ifc = fu_d/(2*Kr)*Gh2
    Pfc = zfc*(Vfc*Ifc/1000)
    
    # PIP in the rerurn side
    Gall = Gab + Gec + Gstu
        
    # MA
    Pmt = zma*(Pmt0 + Pmtf)
    
    # EC
    pc = exp(21.3-2025.5/(248.94+tc))/1e6
    pe = exp(21.3-2025.5/(248.94+te))/1e6
    pr = pc/pe # Compressor
    etavl = 0.98-0.085*(pr**(1/kr)-1)
    etacp = 0.9085*exp(-0.06443*pr)-7.605*exp(-3.155*pr)
    Gr = etavl*Nec*roeg*Vcp
    wi = kr/(1e3*(kr-1))*pe*1e6/roeg*(pr**((kr-1)/kr)-1)
    Pcp = zec*(Gr*wi/etacp)
    
    # BR
    Hpmp = Spmp*Gall**2/(rhow*ge)
    eta_pmp = b0p*Gall**2 + b1p*Gall + b2p
    Ppmp = Gall*ge*Hpmp/(1e3*eta_pmp)
    
    # BA
    iba = Iba/npb
    Vba = nsb*(Em - vcap - R0b*iba)
    Pba = Vba*Iba/1000
    
    # Electric Power
    Psc = Pcp + Ppmp
    Psl = Ppv + Pfc + Pmt + Pba - Psc - Pd
    
    y_alg = Psl
    
    return y_alg

# In[2] Formulate discrete time dynamics {x_f3, xs, x_f1, x_f2, us, u_f1, u_f2, u_f3, uz, ud}
x_sym_f3 = SX.sym('x_f3', Nx_f3)
x_s_sym_f3 = SX.sym('x_s_f3', Nx_s)
x_f1_sym_f3 = SX.sym('x_f1_f3', Nx_f1)
x_f2_sym_f3 = SX.sym('x_f2_f3', Nx_f2)

u_s_sym_f3 = SX.sym('u_s_f3', Nuc_s)
u_f1_sym_f3 = SX.sym('u_f1_f3', Nuc_f1)
u_f2_sym_f3 = SX.sym('u_f2_f3', Nuc_f2)
u_sym_f3 = SX.sym('u_f3', Nuc_f3)

uz_sym_f3 = SX.sym('uz_f3', Nuz_h)
ud_sym_f3 = SX.sym('ud_f3', Nud_f3)

ode_sym_f3 = ode_ies_f3(x_sym_f3, x_s_sym_f3, x_f1_sym_f3, x_f2_sym_f3, 
                        u_s_sym_f3, u_f1_sym_f3, u_f2_sym_f3, u_sym_f3, 
                        uz_sym_f3, ud_sym_f3)

args_f3 = {'x': x_sym_f3, 'p': vertcat(x_s_sym_f3, x_f1_sym_f3, x_f2_sym_f3, 
                                       u_s_sym_f3, u_f1_sym_f3, u_f2_sym_f3, 
                                       u_sym_f3, uz_sym_f3, ud_sym_f3), 'ode': ode_sym_f3}

opts_f3 = {'tf': Delta_f, 'regularity_check': True}
I_ode_f3 = integrator('I_ode_f3', 'rk', args_f3, opts_f3)

